package com.yuncool.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuncool.constants.CommonConstants;
import com.yuncool.domain.YuncoolBbs;
import com.yuncool.domain.vo.BbsRequestVO;
import com.yuncool.mapper.YuncoolBbsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.Date;
import java.util.List;


/**
 * @Auther XIE
 * @Date 2019/9/1 17:26
 * @Description TODO 社区帖子服务类
 * @Version 1.0
 */
@Service
public class YuncoolBbsService {

	@Autowired
	private YuncoolBbsMapper yuncoolBbsMapper;
	@Autowired
	private StringRedisTemplate redisTemplate;

	/**
	 * 获取社区帖子列表
	 *
	 * @param vo
	 * @return
	 */
	public PageInfo<YuncoolBbs> listYunCoolBbs(BbsRequestVO vo) {
		PageHelper.startPage(vo.getPages(), vo.getPageSize());
		Date now = new Date();
		List<YuncoolBbs> yuncoolBbsList = yuncoolBbsMapper.listBbs(vo);
		for (YuncoolBbs yuncoolBbs : yuncoolBbsList) {
			setRedisData(yuncoolBbs);
			/**
			 * 计算权重
			 *
			 * y=0.6*浏览量+0.3*点赞量+0.3*评论数-(当前时间-创建时间)*0.2
			 *
			 * TODO 后期准备定时计算插入表中 有问题待修正
			 */
			yuncoolBbs.setScore(yuncoolBbs.getViews() * 0.6 + yuncoolBbs.getLikeNumber() * 0.3 + yuncoolBbs.getCommentTimes() * 0.3 - ((now.getTime() - yuncoolBbs.getCreateTime().getTime()) / (1000 * 60 * 60 * 24)) * 0.2);
		}
		//排序
		if (CommonConstants.ORDER_BY_TIME.equals(vo.getOrderType())) {
			yuncoolBbsList.sort(Comparator.comparing(YuncoolBbs::getCommentTimes));
		} else if (CommonConstants.ORDER_BY_SCORE.equals(vo.getOrderType())) {
			yuncoolBbsList.sort(Comparator.comparing(YuncoolBbs::getScore));
		} else if (CommonConstants.ORDER_BY_LIKE.equals(vo.getOrderType())) {
			yuncoolBbsList.sort(Comparator.comparing(YuncoolBbs::getLikeNumber));
		} else if (CommonConstants.ORDER_BY_VIEWS.equals(vo.getOrderType())) {
			yuncoolBbsList.sort(Comparator.comparing(YuncoolBbs::getViews));
		}
		PageInfo<YuncoolBbs> pageInfo = new PageInfo<>(yuncoolBbsList);
		return pageInfo;
	}

	public YuncoolBbs getBbs(Integer bbsId) {
		YuncoolBbs yuncoolBbs = yuncoolBbsMapper.selectByPrimaryKey(bbsId);
		setRedisData(yuncoolBbs);
		return yuncoolBbs;
	}

	private void setRedisData(YuncoolBbs yuncoolBbs) {
		if (redisTemplate.opsForHash().hasKey("viewList", yuncoolBbs.getId().toString()) && redisTemplate.opsForHash().hasKey("likeList", yuncoolBbs.getId().toString())) {
			yuncoolBbs.setViews(Integer.parseInt(redisTemplate.opsForHash().get("viewList", yuncoolBbs.getId().toString()).toString()));
			yuncoolBbs.setLikeNumber(Integer.parseInt(redisTemplate.opsForHash().get("likeList", yuncoolBbs.getId().toString()).toString()));
		} else {
			redisTemplate.opsForHash().put("viewList", yuncoolBbs.getId().toString(), "0");
			redisTemplate.opsForHash().put("likeList", yuncoolBbs.getId().toString(), "0");
			yuncoolBbs.setLikeNumber(0);
			yuncoolBbs.setViews(0);
		}
	}

	public void likeBbs(Integer bbsId) {
		if (redisTemplate.opsForHash().hasKey("likeList", bbsId.toString())) {
			//原有记录加一
			redisTemplate.opsForHash().put("likeList", bbsId.toString(), String.valueOf(Integer.parseInt(redisTemplate.opsForHash().get("likeList", bbsId.toString()).toString()) + 1));
		} else {
			redisTemplate.opsForHash().put("likeList", bbsId.toString(), "1");
		}
	}

	public void noLikeBbs(Integer bbsId) {
		if (redisTemplate.opsForHash().hasKey("likeList", bbsId.toString())) {
			Integer i = Integer.parseInt(redisTemplate.opsForHash().get("likeList", bbsId.toString()).toString());
			//原有记录加一
			redisTemplate.opsForHash().put("likeList", bbsId.toString(), String.valueOf(i > 0 ? i - 1 : 0));
		} else {
			redisTemplate.opsForHash().put("likeList", bbsId.toString(), "0");
		}
	}

	public void viewBbs(Integer bbsId) {
		if (redisTemplate.opsForHash().hasKey("viewList", bbsId.toString())) {
			//原有记录加一
			redisTemplate.opsForHash().put("viewList", bbsId.toString(), String.valueOf(Integer.parseInt(redisTemplate.opsForHash().get("viewList", bbsId.toString()).toString()) + 1));
		} else {
			redisTemplate.opsForHash().put("viewList", bbsId.toString(), "1");
		}
	}
}




